package com.controller;

import com.model.DataBean;
import com.model.Dept;
import com.model.Job;
import com.model.JobLog;
import com.learm.service.IDeptService;
import com.learm.service.IJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/job")
public class JobController {

    @Autowired
    private IDeptService deptService;


    @Autowired
    private IJobService jobService;


    @RequestMapping("/find.do")
    public String find(ModelMap model, HttpServletRequest request){
        List<Dept> deptList=deptService.findall();
        //将获取到的数据存入ModelMap中，以便页面取出
        model.addAttribute("depts", deptList);
        return "dept";
    }

    /**
     * 所属系统列表，返回json 格式 list 数据
     * @return
     */
    @RequestMapping("getSysList")
    @ResponseBody
    public List<DataBean> getSysList(){

        return jobService.getSysList();
    }

    /**
     * 类型列表，返回json 格式 list 数据
     * @return
     */
    @RequestMapping("getTypeList")
    @ResponseBody
    public List<DataBean> getTypeList(){

        return jobService.getTypeList();
    }

    /**
     * 状态列表，返回json 格式 list 数据
     * @return
     */
    @RequestMapping("getStatusList")
    @ResponseBody
    public List<DataBean> getStatusList(){

        return jobService.getStatusList();
    }

    /**
     * 人员列表，返回json 格式 list 数据
     * @return
     */
    @RequestMapping("getUserList")
    @ResponseBody
    public List<DataBean> getUserList(){

        return jobService.getUserList();
    }


    /**
     * 添加任务
     * @param job
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("addJob")
    @ResponseBody
    public Map addJob(Job job,HttpServletRequest request, HttpServletResponse response ){
        Map ret = new HashMap();
        String result = this.jobService.addJob(job);
        ret.put("result", result);
        return ret;
    }

    /**
     *
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("deleteJob")
    @ResponseBody
    public Map deleteJob(Long id,HttpServletRequest request, HttpServletResponse response ){
        Map ret = new HashMap();
        String result = this.jobService.deleteJob(id);
        ret.put("result", result);
        return ret;
    }

    /**
     * 根据ID获取任务
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("getJob")
    @ResponseBody
    public Job getJob(Long id,HttpServletRequest request, HttpServletResponse response ){
        Job job = this.jobService.getJobById(id);
        return job;
    }

    /**
     * 任务列表
     * @param sysId
     * @param title
     * @param typeId
     * @param statusId
     * @param currUserId
     * @param pageNo
     * @param pageSize
     * @param orderBy
     * @param orderType
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("getJobList")
    public String getJobList(Integer sysId, String title,Integer typeId,Integer statusId, Long currUserId,
                                   Integer pageNo, Integer pageSize, String orderBy, String orderType,
                                   ModelMap model,HttpServletRequest request, HttpServletResponse response){

        Job queryBean = new Job();
        queryBean.setSysId(sysId);
        if(null != title && !"".equals(title)){

            queryBean.setTitle("%"+title+"%");
        }
        queryBean.setTypeId(typeId);
        queryBean.setStatusId(statusId);
        queryBean.setCurrUserid(currUserId);
        queryBean.setPageNo(pageNo);
        queryBean.setPageSize(pageSize);
        queryBean.setOrderBy(orderBy);
        queryBean.setOrderType(orderType);

        List<Job> jobList = jobService.getJobList(queryBean);
        Integer dataCount = jobService.getJobListSize(queryBean);

        Integer pageCount = 1;
        if(dataCount % queryBean.getPageSize() == 0){
            pageCount = dataCount / queryBean.getPageSize();
        }else{
            pageCount = dataCount / queryBean.getPageSize() + 1;
        }

        model.addAttribute("objs",jobList);
        model.addAttribute("pageSize", queryBean.getPageSize());
        model.addAttribute("pageNo", queryBean.getPageNo());
        model.addAttribute("pageCount", pageCount);
        model.addAttribute("dataCount", dataCount);

        return "job/jobList";

    }

    /**
     * 任务的日志记录
     * @param jobId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("getJobLogList")
    @ResponseBody
    public List<JobLog> getJobLogList(Long jobId,HttpServletRequest request, HttpServletResponse response ){
        return this.jobService.getJobLogList(jobId);
    }

    @RequestMapping("addJobLog")
    @ResponseBody
    public Map addJobLog(JobLog jobLog){
        Map ret = new HashMap();
        String result = this.jobService.addJobLog(jobLog);
        ret.put("result", result);
        return ret;
    }
}
